package cs2340.quarterblack.wheresmystuff;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AdminOptions extends Activity {
	
	Button btnUnlock;
	Button btnManage;
	Button btnDelete;
	Button btnAdd;
	Button btnAdmin;
	EditText userId1;
	EditText userId2;
	EditText userPassword;
	TextView showText;
	Account account;
	

	/**
	 * creates the AdminOptions activity
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_administrative_options);
		// Show the Up button in the action bar.
		setupActionBar();
		account = Model.getAccount();
		btnUnlock = (Button)findViewById(R.id.ci_unlock);
		btnAdd = (Button)findViewById(R.id.ci_addUser);
		btnManage = (Button)findViewById(R.id.ci_privelages);
		btnDelete = (Button)findViewById(R.id.ci_remove);
		btnAdmin = (Button)findViewById(R.id.ci_addAdmin);
		userId1 = (EditText)findViewById(R.id.ci_uid);
		userId2 = (EditText)findViewById(R.id.ci_uid2);
		userPassword = (EditText)findViewById(R.id.ci_password);
		showText = (TextView)findViewById(R.id.ci_showOutput);
		TextWatcher watcher = new CopyCat(userId2);
		userId1.addTextChangedListener(watcher);
	
	/**
	 * sets up the unlock button to unlock an account
	 */
	btnUnlock.setOnClickListener(new OnClickListener(){

		@Override
		public void onClick(View arg0) {
			boolean stat;
			stat =account.unlockAccount(getId());
			if(stat == true)
			{
				setOutput("account " + getId() + " unlocked");
			}
			else
			{
				setOutput("account " + getId() + " does not exist");
			}
		}
		
			});
	/**
	 * sets up the add button
	 */
	btnAdd.setOnClickListener(new OnClickListener(){
		
		public void onClick(View arg0)
		{
			Account account = new Account(getId2(),getPassword());
			if(account.addUser() == true)
			{
				setOutput("account " + getId() + " added");
			}
			else
			{
				setOutput("failed to add " + getId2() + ". The account may already exist");
			}
		}		
	});
	
	/**
	 * sets up the add admin button
	 */
	btnAdmin.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Account account = new Account(getId2(), getPassword(), true);
			if(account.addUser() == true)
				setOutput("account " + getId() + " added");
			else
				setOutput("failed to add " + getId2() + ". The account may already exist");
			
		}
	});
	
	
	/**
	 * sets up the delete button
	 */
	btnDelete.setOnClickListener(new OnClickListener(){
		
		public void onClick(View view)
		{
			if(account.deleteAccount(getId()) == true)
			{
				setOutput("account " + getId() + " deleted");
			}
			else
			{
				setOutput("failed to delete " + getId() + ". Maybe the account doesn't exist");
			}
		}
	});
	/**
	 * sets up the manage privelages button
	 */
	btnManage.setOnClickListener(new OnClickListener(){
		
		public void onClick(View view)
		{
			if(account.checkExists(getId()) == true)
			{
				setOutput("changing " + getId() + "'s privelages");
				Intent intent = new Intent(AdminOptions.this,Privelages.class);
				intent.putExtra("userId", getId());
				AdminOptions.this.startActivity(intent);
			}
			else
			{
				setOutput("User does not exist");
			}
		}
	});
	}
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.just_logout, menu);
		return true;
		
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		case R.id.action_logout:
			Intent homepage = new Intent(AdminOptions.this,LoginPage.class);
			homepage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			AdminOptions.this.startActivity(homepage);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	/**
	 * gets the user Id from the text field
	 * @return the entered user id
	 */
	protected String getId()
	{
		return userId1.getText().toString();
	}
	/**
	 * gets the user Id from the second Id text field
	 * @return the entered user id
	 */
	protected String getId2()
	{
		return userId2.getText().toString();
	}
	/**
	 * gets the user password from the text field
	 * @return the enetered user password
	 */
	protected String getPassword()
	{
		return userPassword.getText().toString();
	}
	/**
	 * sets the outuput of the output text field
	 * @param string the value the text field should display
	 */
	protected void setOutput(String string)
	{
		Toast.makeText(getApplicationContext(), string,5).show();
	}
	/**
	 * used to copy the data from the first text field into the second.
	 *
	 */
	private class CopyCat implements TextWatcher
	{

		private EditText text1;
		//private EditText text2;
		public CopyCat(EditText text1)
		{
			this.text1 = text1;
			//this.text2 = text2;
		}
		/**
		 * copys the data from text field 1 to text field 2
		 */
		@Override
		public void afterTextChanged(Editable arg0) {
			text1.setText(arg0.toString());
		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
			
		}
		
	}

}
